Add autopkgtest
authorShengjing Zhu <zhsj@debian.org>
Sat, 6 Mar 2021 10:53:13 +0000 (18:53 +0800)
committerShengjing Zhu <zhsj@debian.org>
Sat, 6 Mar 2021 11:09:46 +0000 (19:09 +0800)
debian/tests/CMakeLists.txt [new file with mode: 0644]
debian/tests/CommandLineConvertTest.cpp [new file with mode: 0644]
debian/tests/README.md [new file with mode: 0644]
debian/tests/control [new file with mode: 0644]
debian/tests/integration [new file with mode: 0755]

diff --git a/debian/tests/CMakeLists.txt b/debian/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..7c971f3
--- /dev/null
@@ -0,0 +1,35 @@
+cmake_minimum_required(VERSION 3.18)
+project (opencc-integration CXX)
+include (CTest)
+enable_testing()
+
+find_package(PkgConfig REQUIRED)
+find_program (OPENCC_TOOL opencc REQUIRED)
+pkg_check_modules(OPENCC REQUIRED IMPORTED_TARGET opencc)
+
+add_definitions(
+  -DCMAKE_BINARY_DIR="${CMAKE_BINARY_DIR}"
+  -DCMAKE_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
+  -DPROJECT_BINARY_PATH="${OPENCC_TOOL}"
+)
+set(CONFIG_TEST
+  ../../test/config_test/config_test.json
+  ../../test/config_test/config_test_characters.txt
+  ../../test/config_test/config_test_phrases.txt
+)
+
+set(CONFIG_TEST_TARGET_DIR ${PROJECT_BINARY_DIR}/test/config_test)
+make_directory(${CONFIG_TEST_TARGET_DIR})
+foreach (CONFIG_TEST_FILE ${CONFIG_TEST})
+  configure_file(${CONFIG_TEST_FILE} ${CONFIG_TEST_TARGET_DIR} COPYONLY)
+endforeach (CONFIG_TEST_FILE)
+
+add_subdirectory(/usr/src/googletest/googletest ${CMAKE_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL)
+set(UNITTESTS
+  CommandLineConvertTest
+)
+foreach(UNITTEST ${UNITTESTS})
+  add_executable(${UNITTEST} ${UNITTEST}.cpp)
+  target_link_libraries(${UNITTEST} gtest gtest_main PkgConfig::OPENCC)
+  add_test(${UNITTEST} ${UNITTEST})
+endforeach(UNITTEST)
diff --git a/debian/tests/CommandLineConvertTest.cpp b/debian/tests/CommandLineConvertTest.cpp
new file mode 100644 (file)
index 0000000..6ca6068
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Open Chinese Convert
+ *
+ * Copyright 2015 Carbo Kuo <byvoid@byvoid.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fstream>
+
+#include "Common.hpp"
+#include "gtest/gtest.h"
+
+namespace opencc {
+
+class CommandLineConvertTest : public ::testing::Test {
+protected:
+  CommandLineConvertTest() { GetCurrentWorkingDirectory(); }
+
+  virtual ~CommandLineConvertTest() { free(originalWorkingDirectory); }
+
+  virtual void SetUp() {
+    ASSERT_NE("", CMAKE_BINARY_DIR);
+    ASSERT_NE("", CMAKE_SOURCE_DIR);
+    ASSERT_EQ(0, chdir("/usr/share/opencc/"));
+  }
+
+  virtual void TearDown() { ASSERT_EQ(0, chdir(originalWorkingDirectory)); }
+
+  std::string GetFileContents(const std::string& fileName) const {
+    std::ifstream fs(fileName);
+    EXPECT_TRUE(fs.is_open());
+    const std::string content((std::istreambuf_iterator<char>(fs)),
+                              (std::istreambuf_iterator<char>()));
+    fs.close();
+    return content;
+  }
+
+  void GetCurrentWorkingDirectory() {
+    originalWorkingDirectory = getcwd(nullptr, 0);
+  }
+
+  const char* OpenccCommand() const {
+    return PROJECT_BINARY_PATH;
+  }
+
+  const char* InputDirectory() const {
+    return CMAKE_SOURCE_DIR "/../../test/testcases/";
+  }
+
+  const char* OutputDirectory() const { return CMAKE_BINARY_DIR "/test/"; }
+
+  const char* AnswerDirectory() const {
+    return CMAKE_SOURCE_DIR "/../../test/testcases/";
+  }
+
+  const char* ConfigurationDirectory() const {
+    return "/usr/share/opencc/";
+  }
+
+  std::string OutputFile(const char* config) const {
+    return std::string(OutputDirectory()) + config + ".out";
+  }
+
+  std::string AnswerFile(const char* config) const {
+    return std::string(AnswerDirectory()) + config + ".ans";
+  }
+
+  std::string TestCommand(const char* config) const {
+    return OpenccCommand() + std::string("") + " -i " + InputDirectory() +
+           config + ".in" + " -o " + OutputFile(config) + " -c " +
+           ConfigurationDirectory() + config + ".json";
+  }
+
+  char* originalWorkingDirectory;
+};
+
+class ConfigurationTest : public CommandLineConvertTest,
+                          public ::testing::WithParamInterface<const char*> {};
+
+TEST_P(ConfigurationTest, Convert) {
+  const char* config = GetParam();
+  ASSERT_EQ(0, system(TestCommand(config).c_str()));
+  const std::string& output = GetFileContents(OutputFile(config));
+  const std::string& answer = GetFileContents(AnswerFile(config));
+  ASSERT_EQ(answer, output);
+}
+
+INSTANTIATE_TEST_CASE_P(CommandLine, ConfigurationTest,
+                        ::testing::Values("hk2s", "hk2t", "jp2t", "s2hk", "s2t",
+                                          "s2tw", "s2twp", "t2hk", "t2jp", "t2s",
+                                          "tw2s", "tw2sp", "tw2t"));
+
+} // namespace opencc
diff --git a/debian/tests/README.md b/debian/tests/README.md
new file mode 100644 (file)
index 0000000..12d6cec
--- /dev/null
@@ -0,0 +1,3 @@
+Fork from ../../test, please refresh this file if source has changed.
+
+Test with installed opencc tool and library.
diff --git a/debian/tests/control b/debian/tests/control
new file mode 100644 (file)
index 0000000..4611398
--- /dev/null
@@ -0,0 +1,7 @@
+Tests: integration
+Depends:
+ cmake,
+ googletest,
+ pkg-config,
+ @,
+Restrictions: allow-stderr,
diff --git a/debian/tests/integration b/debian/tests/integration
new file mode 100755 (executable)
index 0000000..c1f1746
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+set -ex
+
+cd "$(dirname "$0")"
+mkdir -p build
+cd build
+cmake ..
+make
+make test
+cd ..
+rm -rf build